package be.kuleuven.mume.beans;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import be.kuleuven.mume.PMF;
import be.kuleuven.mume.query.DeleteQueryHelper;
import be.kuleuven.mume.query.FetchQueryHelper;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class VraagBean{
	
	//@NotPersistent
	//private final static Logger log = Logger.getLogger(VraagBean.class.getName());
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;
	@Persistent
	private Key vak;
	@Persistent
	private String username;
	@Persistent
	private Key userid;
	@Persistent
	private String subject;
	@Persistent
	private String text;
	@Persistent
	private Date date;
	@Persistent
	private List<Key> antwoorden;

	
	public VraagBean(PersoonBean fromPersoon, VakBean vak, String subject, String text){
		
		setUsername("");
		setUserid(null);
		//this.setUsername(fromPersoon.getUsername());
		//this.setUserid(fromPersoon.getId());
		this.vak = KeyFactory.stringToKey(vak.getVakId());
		this.text = text;
		this.subject = subject;
		this.antwoorden = new ArrayList<Key>();
		this.date = Calendar.getInstance().getTime();
	}
	
	public Key getVraagKey() {
		return this.id;
	}
	
	public void setId(String key) {
		this.id = KeyFactory.stringToKey(key);
	}

	public String getId() {
		return KeyFactory.keyToString(id);
	}

	public void setVakId(String vakId) {
		this.vak = KeyFactory.stringToKey(vakId);
	}

	public String getVakId() {
		return KeyFactory.keyToString(vak);
	}
	
	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setUserid(Key userid) {
		this.userid = userid;
	}

	public Key getUserid() {
		return userid;
	}
	
	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getSubject() {
		return subject;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getText() {
		return text;
	}

	public void addAntwoord(Key a)
	{
		this.antwoorden.add(a);
	}
	
	public void setAntwoorden(List<Key> antwoorden) {
		this.antwoorden = antwoorden;
	}

	public List<Key> getAntwoorden() {
		return antwoorden;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDate() {
		return date;
	}

	public String toString(){
		StringBuilder str = new StringBuilder();
		str.append("From: ");
		str.append(username);
		str.append(" onderwerp ");
		str.append(this.getSubject());
		str.append(" Vraag: ");
		str.append(this.getText());
		str.append("\nAntwoordenKey:\n");
		for (Key a : this.antwoorden) {
			str.append("\t");
			str.append(KeyFactory.keyToString(a));
			str.append("\n");
		}
		return str.toString();
	}
	
	public void store()
	{
		PersistenceManager pm = PMF.getPersistenceManager();
		pm.makePersistent(this);
		pm.close();
	}
	
	public void delete(){
		DeleteQueryHelper h = new DeleteQueryHelper(this.getId(), this.getClass());
		h.executeAtomicQuery();
	}

	public static class VraagBeanFactory{
		public static VraagBean getVraagById(String vraagId){
			FetchQueryHelper<VraagBean> h = new FetchQueryHelper<VraagBean>(vraagId){};
			return h.executeAtomicQuery();
		}
		
		@SuppressWarnings("unchecked")
		public static List<VraagBean> getVragenFromVak(String vakId, long page, long rpp){
			PersistenceManager pm = PMF.getPersistenceManager();
			Query q = pm.newQuery(VraagBean.class);
			q.setRange(page*rpp,(page+1)*rpp);
			q.setFilter("vak == vakParam");
			q.setOrdering("date descending");
			q.declareImports("import com.google.appengine.api.datastore.Key");
			q.declareParameters("Key vakParam");
			return (List<VraagBean>)q.execute(KeyFactory.stringToKey(vakId));
		}
		
		@SuppressWarnings("unchecked")
		public static List<VraagBean> getVragen(long rpp, long page) {
			PersistenceManager pm = PMF.getPersistenceManager();
			Query query = pm.newQuery(VraagBean.class);
			query.setRange(page*rpp, (page+1)*rpp);
			return (List<VraagBean>)query.execute();
		}
	}
}
